package com.yanqu.road.server.cmd.cross3.xrank.season;

import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.logic.season.BaseSeasonFactory;
import com.yanqu.road.server.logic.season.BaseSeasonRank;
import com.yanqu.road.server.logic.season.BaseSeasonReward;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_XRANK_GET_SEASON_REWARD, desc = "领取赛季排行奖励")
public class GetXRankSeasonRewardCmd extends AbstractCommand {

    private int type;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        XRankProto.GetXRankSeasonRewardReqMsg reqMsg = XRankProto.GetXRankSeasonRewardReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 拿一下配置
        eSeasonRewardType enumType = eSeasonRewardType.getType(type);
        if (enumType == null) {
            ret = GameErrorCode.E_SEASON_REWARD_TYPE_NO_EXIST;
            return;
        }
        BaseSeasonReward tmpInstance = BaseSeasonFactory.getInstance(enumType, player);
        if (!(tmpInstance instanceof BaseSeasonRank)) {
            ret = GameErrorCode.E_SEASON_REWARD_TYPE_NO_EXIST;
            return;
        }
        BaseSeasonRank instance = (BaseSeasonRank) tmpInstance;

        // 没到领奖时间
        int tmpRet = instance.inGetRewardTime();
        if (tmpRet != 0) {
            ret = tmpRet;
            return;
        }

        // 没有数据
        if (instance.getData() == null) {
            ret = GameErrorCode.E_SEASON_REWARD_NO_COMPLETE;
            return;
        }

        // 已经领过奖
        if (instance.getData().isGetReward()) {
            ret = GameErrorCode.E_SEASON_REWARD_HAD_GET_REWARD;
            return;
        }

        // 到跨服，再到跨地区去拿排名
        XRankProto.CrossGetXRankSeasonRewardReqMsg.Builder builder = XRankProto.CrossGetXRankSeasonRewardReqMsg.newBuilder();
        builder.setSystemId(instance.getSystemId());
        builder.setSeasonId(instance.getSeasonId());
        builder.setType(instance.getType().getIntValue());
        builder.setRangeKey(instance.getRangeKey());
        builder.setChannelId(ServerListMgr.getChannelId(GameServer.getInstance().getServerId()));
        builder.setServerId(GameServer.getInstance().getServerId());
        builder.setUserId(player.getUserId());
        player.sendPacket(Cross3Protocol.C3_XRANK_GET_SEASON_REWARD, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            XRankProto.GetXRankSeasonRewardRespMsg.Builder builder = XRankProto.GetXRankSeasonRewardRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_XRANK_GET_SEASON_REWARD, builder);
        }
    }
}
